package space.enemy;

import java.util.ArrayList;

import org.anddev.andengine.collision.BaseCollisionChecker;
import org.anddev.andengine.engine.Engine;
import org.anddev.andengine.engine.camera.Camera;
import org.anddev.andengine.engine.handler.IUpdateHandler;
import org.anddev.andengine.engine.handler.collision.CollisionHandler;
import org.anddev.andengine.engine.handler.collision.ICollisionCallback;
import org.anddev.andengine.engine.handler.physics.PhysicsHandler;
import org.anddev.andengine.engine.handler.timer.ITimerCallback;
import org.anddev.andengine.engine.handler.timer.TimerHandler;
import org.anddev.andengine.engine.options.EngineOptions;
import org.anddev.andengine.engine.options.EngineOptions.ScreenOrientation;
import org.anddev.andengine.engine.options.resolutionpolicy.RatioResolutionPolicy;
import org.anddev.andengine.entity.scene.Scene;
import org.anddev.andengine.entity.scene.background.ColorBackground;
import org.anddev.andengine.entity.shape.IShape;
//import org.anddev.andengine.entity.scene.background.AutoParallaxBackground;
//import org.anddev.andengine.entity.scene.background.ColorBackground;
//import org.anddev.andengine.entity.scene.background.ParallaxBackground.ParallaxEntity;
import org.anddev.andengine.entity.sprite.AnimatedSprite;
import org.anddev.andengine.entity.sprite.Sprite;
import org.anddev.andengine.entity.text.ChangeableText;
import org.anddev.andengine.entity.util.FPSLogger;



import org.anddev.andengine.opengl.font.Font;
import org.anddev.andengine.opengl.texture.Texture;
import org.anddev.andengine.opengl.texture.TextureOptions;
import org.anddev.andengine.opengl.texture.region.TextureRegion;
import org.anddev.andengine.opengl.texture.region.TextureRegionFactory;
import org.anddev.andengine.opengl.texture.region.TiledTextureRegion;
import org.anddev.andengine.sensor.accelerometer.AccelerometerData;
import org.anddev.andengine.sensor.accelerometer.IAccelerometerListener;
import org.anddev.andengine.ui.activity.BaseGameActivity;

import space.enemy.ParallaxBackground.ParallaxEntity;

import android.graphics.Color;
import android.graphics.Typeface;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Debug;
import android.widget.Toast;


public class AnimatedSpritesExample2 extends BaseGameActivity implements SensorEventListener {
	// ===========================================================
	// Constants
	// ===========================================================

	public static final int CAMERA_WIDTH = 480;
	public static final int CAMERA_HEIGHT = 320;
	static final int BORDER_SIZE = 50;
	// ===========================================================
	// Fields
	// ===========================================================

	private Camera mCamera;

	private Texture mTexture;

	private TiledTextureRegion mSnapdragonTextureRegion;
	private TiledTextureRegion mHelicopterTextureRegion;
	private TiledTextureRegion mBananaTextureRegion;
	private TiledTextureRegion mFaceTextureRegion;
	private Texture mAutoParallaxBackgroundTexture;
	private TextureRegion mParallaxLayerFront;
	private Texture mTextureAlieni;
	private TiledTextureRegion mAlieniTextureRegion;
	private TextureRegion mParallaxLayerCloud;
	private PhysicsHandler physicsHandlerSpace;
	private PhysicsHandler physicsHandler;
	private int count=0;
	private float x;
	//private AnimatedSprite snapdragon;
    private int sX=100; // Sprite coordinates
	private SensorManager sensorManager;
	private ArrayList<SpaceEnemy> se;
	private EnemyManager enemyManager;
	private BulletManager bulletManager;
	private Texture mBulletTexture;
	private TextureRegion mBulletTextureRegion;
	private Texture mFireButtonTexture;
	private TextureRegion mFireButtonTextureRegion;
	private Shuttle shuttle;
	private Texture mTextureAlieniPar;
	private TiledTextureRegion mAlieniTextureRegionPar;
	//private EnemyStrong enemyStrong;
	private AutoParallaxBackground autoParallaxBackground;
	private ParallaxEntity parallaxLayerFront;
	private ParallaxEntity parallaxLayerCloud;
	private ChangeableText mText;
	private SpaceEnemy temp;
	private TiledTextureRegion mAlieniRottiTextureRegion;
	private SpaceEnemy temp2;
	private Texture mTextureAlieniRotti;
	protected boolean cancella;
	private Scene scene;
	ArrayList<SpaceEnemy> enemyList = new ArrayList<SpaceEnemy>();

	// ===========================================================
	// Constructors
	// ===========================================================

	// ===========================================================
	// Getter & Setter
	// ===========================================================

	// ===========================================================
	// Methods for/from SuperClass/Interfaces
	// ===========================================================

	@Override
	public Engine onLoadEngine() {
		this.mCamera = new Camera(0, 0, CAMERA_WIDTH, CAMERA_HEIGHT);
		return new Engine(new EngineOptions(true, ScreenOrientation.LANDSCAPE, new RatioResolutionPolicy(CAMERA_WIDTH, CAMERA_HEIGHT), this.mCamera));
	}

	@Override
	public void onLoadResources() {
		
	
		this.mTextureAlieni = new Texture(256, 64, TextureOptions.BILINEAR_PREMULTIPLYALPHA);
		this.mAlieniTextureRegion = TextureRegionFactory.createTiledFromAsset(this.mTextureAlieni, this, "gfx/alieni step 5-03.png", 0, 0, 4, 1);
		
		this.mTextureAlieniRotti = new Texture(256, 64, TextureOptions.BILINEAR_PREMULTIPLYALPHA);		
		this.mAlieniRottiTextureRegion = TextureRegionFactory.createTiledFromAsset(this.mTextureAlieniRotti, this, "gfx/alieni_rotti.png", 0, 0, 2, 1);
		
		this.mEngine.getTextureManager().loadTextures(this.mTextureAlieni,mTextureAlieniRotti);


	}

	@Override
	public Scene onLoadScene() {
	
		
		
		this.mEngine.registerUpdateHandler(new FPSLogger());

		scene = new Scene(1);
        scene.setBackground(new ColorBackground(0, 0, 0));
        temp = new SpaceEnemy(20,5, mAlieniTextureRegion,100,scene,10);
        enemyList.add(temp);
 		
 		return scene;
	}

	

	@Override
	public void onLoadComplete() {
		this.mEngine.registerUpdateHandler(new IUpdateHandler() {
            public void onUpdate(float pSecondsElapsed) {
            	if(cancella){
            		int x;
            		int y;
            		
            				//cancello
    	    				mEngine.getScene().getLastChild().detachChild(enemyList.get(0));
    	    				x = (int) (enemyList.get(0).getX()+enemyList.get(0).getWidth()/2);
    	    				y= (int) enemyList.get(0).getY();
    	    				enemyList.remove(enemyList.get(0));	        				
    	    				
    	    				cancella = false;
    	          		temp2 = new SpaceEnemy(x,y, mAlieniRottiTextureRegion,100,scene,10);
            		enemyList.add(temp2);
            	}
            	for(SpaceEnemy sp:enemyList){            	
            		sp.update(false);
            	}
            	
            	
         }
            public void reset() {
            }
        });
		this.mEngine.registerUpdateHandler( new TimerHandler(10.0f, new ITimerCallback() {
			
			@Override
			public void onTimePassed(TimerHandler pTimerHandler) {
					pTimerHandler.reset();
					cancella = true;
								
			}
			
		}));
		
	
	}



	@Override
	public void onAccuracyChanged(Sensor sensor, int accuracy) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onSensorChanged(SensorEvent event) {
		   switch (event.sensor.getType()) {
           case Sensor.TYPE_ACCELEROMETER:
               this.x = (int)event.values[1];
       		//System.out.println("ACCELLERAZIONE2:"+(int)event.values[1]);

               //accellerometerSpeedY = (int)event.values[0];
               break;
       }
   }
	static class CollisionManager implements ICollisionCallback {
    	
		@Override
		public boolean onCollision(IShape pCheckShape, IShape pTargetShape) {
			
			Shuttle ship = (Shuttle) pCheckShape;
			//System.out.println("COLLSION");
			if  ( ship.isAlive() && checkCollisions(pCheckShape,pTargetShape)) {
				

				ship.setLife(0);
				SpaceEnemy enemy = (SpaceEnemy) pTargetShape;
				//enemy.decreaseLife(SpaceEnemy.HEALTH);
				return true;//blocca il collision handler
			}
			
			return false; 
		

			
		}
				
		public static boolean checkCollisions(IShape pCheckShape, IShape pTargetShape) {
			return BaseCollisionChecker.checkAxisAlignedRectangleCollision( pCheckShape.getX(), pCheckShape.getY(), 
					pCheckShape.getX() + pCheckShape.getWidth(), pCheckShape.getY() + pCheckShape.getHeight(),
					pTargetShape.getX(), pTargetShape.getY(), 
					pTargetShape.getX() + pTargetShape.getWidth(), pTargetShape.getY() + pTargetShape.getHeight());
					
		}
    	
    }
	/*@Override
	public void onUnloadResources() {
	  Debug.stopMethodTracing();
	}*/

	
	
	}
		

